<?php
/**
 * Project in naz.
 * User: linzh
 * Date: 2017-04-27
 * Time: 14:47
 */
declare(strict_types=1);


namespace application\module\explorer\libs;

/**
 * 类名：CreatMiniature
 * 功能：生成多种类型的缩略图
 * 基本参数：$srcfile,$echotype
 * 方法用到的参数：
 * $tofile,生成的文件 * $tow,生成的宽  $toh,生成的高*
 * $bk1,背景颜色参数 以255为最高 * $bk2,背景颜色参数 * $bk3,背景颜色参数
 *
 * 例子：
 * include('thumb.php');
 * $cm=new creatminiature();
 * $cm->setvar('1.jpg','file');
 * $cm->distortion('dis_bei.jpg',150,200);
 * $cm->prorate('pro_bei.jpg',150,200);//附带切割
 * $cm->cut('cut_bei.jpg',150,200);
 * $cm->backfill('fill_bei.jpg',150,200);
 */
class CreatMiniature
{
    // 公共变量
    private $srcfile = '';    //原图
    private $echotype;        //输出图片类型，link--不保存为文件；file--保存为文件
    /**
     * @var resource
     */
    private $im = null;        //临时变量
    private $srcw = '';        //原图宽
    private $srch = '';        //原图高

    // 设置变量及初始化
    public function setvar($srcfile, $echotype)
    {
        $this->srcfile = $srcfile;
        $this->echotype = $echotype;

        $info = '';
        $data = getimagesize($this->srcfile, $info);
        switch ($data[2]) {
            case 1:
                if (!function_exists('imagecreatefromgif')) {
                    exit();
                }
                $this->im = imagecreatefromgif($this->srcfile);
                break;
            case 2:
                if (!function_exists('imagecreatefromjpeg')) {
                    exit();
                }
                $this->im = imagecreatefromjpeg($this->srcfile);
                break;
            case 3:
                $this->im = imagecreatefrompng($this->srcfile);
                break;
        }
        $this->srcw = imagesx($this->im);
        $this->srch = imagesy($this->im);
    }

    // 生成扭曲型缩图
    public function distortion($tofile, $tow, $toh)
    {
        $cimg = $this->creatimage($this->im, $tow, $toh, 0, 0, 0, 0, $this->srcw, $this->srch);
        return $this->echoimage($cimg, $tofile);
    }

    // 生成按比例缩放的缩图
    public function prorate($tofile, $tow, $toh)
    {
        $towh = $tow / $toh;
        $srcwh = $this->srcw / $this->srch;
        if ($towh <= $srcwh) {
            $ftow = $tow;
            $ftoh = $ftow * ($this->srch / $this->srcw);
        } else {
            $ftoh = $toh;
            $ftow = $ftoh * ($this->srcw / $this->srch);
        }
        if ($this->srcw > $tow || $this->srch > $toh) {
            $cimg = $this->creatimage($this->im, $ftow, $ftoh, 0, 0, 0, 0, $this->srcw, $this->srch);
            return $this->echoimage($cimg, $tofile);
        } else {
            $cimg = $this->creatimage($this->im, $this->srcw, $this->srch, 0, 0, 0, 0, $this->srcw, $this->srch);
            return $this->echoimage($cimg, $tofile);
        }
    }

    // 生成最小裁剪后的缩图
    public function cut($tofile, $tow, $toh)
    {
        $towh = $tow / $toh;
        $srcwh = $this->srcw / $this->srch;
        if ($towh <= $srcwh) {
            $ctoh = $toh;
            $ctow = $ctoh * ($this->srcw / $this->srch);
        } else {
            $ctow = $tow;
            $ctoh = $ctow * ($this->srch / $this->srcw);
        }
        $allimg = $this->creatimage($this->im, $ctow, $ctoh, 0, 0, 0, 0, $this->srcw, $this->srch);
        $cimg = $this->creatimage($allimg, $tow, $toh, 0, 0, ($ctow - $tow) / 2, ($ctoh - $toh) / 2, $tow, $toh);
        return $this->echoimage($cimg, $tofile);
    }

    // 生成背景填充的缩图,默认用白色填充剩余空间，传入$isalpha为真时用透明色填充
    public function backfill($tofile, $tow, $toh, $isalpha = false, $red = 255, $green = 255, $blue = 255)
    {
        $towh = $tow / $toh;
        $srcwh = $this->srcw / $this->srch;
        if ($towh <= $srcwh) {
            $ftow = $tow;
            $ftoh = $ftow * ($this->srch / $this->srcw);
        } else {
            $ftoh = $toh;
            $ftow = $ftoh * ($this->srcw / $this->srch);
        }
        if (function_exists('imagecreatetruecolor')) {
            @$cimg = imagecreatetruecolor($tow, $toh);
            if (!$cimg) {
                $cimg = imagecreate($tow, $toh);
            }
        } else {
            $cimg = imagecreate($tow, $toh);
        }


        $fromtop = ($toh - $ftoh) / 2;//从正中间填充
        $backcolor = imagecolorallocate($cimg, $red, $green, $blue); //填充的背景颜色
        if ($isalpha) {//填充透明色
            $backcolor = imagecolortransparent($cimg, $backcolor);
            $fromtop = $toh - $ftoh;//从底部填充
        }

        imagefilledrectangle($cimg, 0, 0, $tow, $toh, $backcolor);
        if ($this->srcw > $tow || $this->srch > $toh) {
            $proimg = $this->creatimage($this->im, $ftow, $ftoh, 0, 0, 0, 0, $this->srcw, $this->srch);
            if ($ftow < $tow) {
                imagecopy($cimg, $proimg, ($tow - $ftow) / 2, 0, 0, 0, $ftow, $ftoh);
            } else if ($ftoh < $toh) {
                imagecopy($cimg, $proimg, 0, $fromtop, 0, 0, $ftow, $ftoh);
            } else {
                imagecopy($cimg, $proimg, 0, 0, 0, 0, $ftow, $ftoh);
            }
        } else {
            imagecopymerge($cimg, $this->im, ($tow - $ftow) / 2, $fromtop, 0, 0, $ftow, $ftoh, 100);
        }
        return $this->echoimage($cimg, $tofile);
    }

    public function creatimage($img, $creatw, $creath, $dstx, $dsty, $srcx, $srcy, $srcimgw, $srcimgh)
    {
        if (function_exists('imagecreatetruecolor')) {
            @$creatimg = imagecreatetruecolor($creatw, $creath);
            if ($creatimg)
                imagecopyresampled($creatimg, $img, $dstx, $dsty, $srcx, $srcy, $creatw, $creath, $srcimgw, $srcimgh);
            else {
                $creatimg = imagecreate($creatw, $creath);
                imagecopyresized($creatimg, $img, $dstx, $dsty, $srcx, $srcy, $creatw, $creath, $srcimgw, $srcimgh);
            }
        } else {
            $creatimg = imagecreate($creatw, $creath);
            imagecopyresized($creatimg, $img, $dstx, $dsty, $srcx, $srcy, $creatw, $creath, $srcimgw, $srcimgh);
        }
        return $creatimg;
    }

    // 输出图片，link---只输出，不保存文件。file--保存为文件
    public function echoimage($img, $to_file)
    {
        switch ($this->echotype) {
            case 'link':
                return imagepng($img);
                break;
            case 'file':
                return imagepng($img, $to_file);
                break;
            //return imagejpeg($img, $to_file);
        }
        return null;
    }
}